package com.uchengwang.ui.cm_ui.cycle.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWhitTitleNoBackActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/5/27.
 */

public class ApplyCycleActivity extends BaseWhitTitleNoBackActivity implements View.OnClickListener{
    private RelativeLayout apply_cycle_rl, apply_name_rl, apply_desc_rl;
    private TextView apply_cycle_name,apply_name, apply_desc;
    private Button apply_ok;

    private String nameString, descString;
    private long cycleId;
    private String cycleName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_apply_cycle);
        cycleId = getIntent().getLongExtra(BaseConfig.ID, 0);
        cycleName = getIntent().getStringExtra(BaseConfig.PUB_TITLE);
        setTitle("申请加入");

        initView();
    }

    private void initView(){
        apply_cycle_rl = (RelativeLayout) findViewById(R.id.apply_cycle_rl);

        apply_name_rl = (RelativeLayout) findViewById(R.id.apply_name_rl);
        apply_name_rl.setOnClickListener(this);

        apply_desc_rl = (RelativeLayout) findViewById(R.id.apply_desc_rl);
        apply_desc_rl.setOnClickListener(this);

        apply_cycle_name = (TextView) findViewById(R.id.apply_cycle_name);
        apply_name = (TextView) findViewById(R.id.apply_name);
        apply_desc = (TextView) findViewById(R.id.apply_desc);
        apply_ok = (Button) findViewById(R.id.apply_ok);
        apply_ok.setOnClickListener(this);

        apply_cycle_name.setText(cycleName);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.apply_name_rl:
                DialogUtils.getUtils().showEditWindow(this, "申请人姓名", nameString, 10, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (!TextUtils.isEmpty(data)){
                            nameString = data;
                            apply_name.setText(data);
                        }
                    }
                });

                break;
            case R.id.apply_desc_rl:
                DialogUtils.getUtils().showEditWindow(this, "申请备注", descString, 50, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (!TextUtils.isEmpty(data)){
                            descString = data;
                            apply_desc.setText(data);
                        }
                    }
                });
                break;
            case R.id.apply_ok:
                if (TextUtils.isEmpty(nameString)){
                    UCommonUtil.getUtil().showToast(this, "请输入姓名");
                    return;
                }
                CycleHttpUtils.getUtils().joinCycle(this, cycleId, nameString, descString, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            UCommonUtil.getUtil().showToast(ApplyCycleActivity.this, "申请成功, 等待圈主审核");
                            CycleBean cycleBean = new CycleBean();
                            cycleBean.setId(cycleId);
                            EventBus.getDefault().post(cycleBean);

                            ApplyCycleActivity.this.finish();
                        }else {
                            UCommonUtil.getUtil().showToast(ApplyCycleActivity.this, "" +
                                    "请重试");
                        }
                    }
                });
                break;
        }
    }
}
